<?php 
/******************************************************************************************
Use Case name and Id: 1.0 View Group Ride Event 
by Michael Puzon,  lastupdated March 21, 2010
Class: COIN 71

Functional Specification

The php servlet issues a feed request to the Google Calendar API using the specified user google username and password. Upon successful connection, data is then outputted in json format.

Web Master Maintenance:
******************************************************************************************
Web master may update the google username and password used to connect to Google Calendar. The parameters are under the $user and $pass variable, near the end of the page.
*/
    
    //this function takes away \n in a string and replaces with n3wline text
	//this is useful in putting multiple fields in a single json field
    function remLine($str){
		return str_replace("\n", " n3wline ", $str);
	}
	
	//outputs error message in json format
	function errormsg($errormsg){	
		echo 	'{ '
			. '"success": false, ' 
     		        . ' "message": "' . $errormsg . '" '
     		     	. ' }';
	}
    
//gets events feed from Google Calendar object
//outputs each field in json format
//before output, multiple line content feed is passed to remLine function
//client - Zend_Gdata_ClientLogin object used to connect to google calendar
function outputCalendar($client) {
  $gdataCal = new Zend_Gdata_Calendar($client);
  $eventFeed = $gdataCal->getCalendarEventFeed();
  echo '{ '
	. '"success": true, ' 
	. '"fields" : [' ;
  $comma = "";
  foreach ($eventFeed as $event) {
  	echo $comma;
	echo    '{ "title": "' . $event->title->text . '", ' 
		. ' "detail": "' . remLine($event->content) . '", ' . "\n"; 
	
	foreach ($event->when as $when) {
		echo ' "date": "' . $when->startTime . '" , ' . "\n";
    	}
    	
    foreach ($event->where as $where) {
    	echo ' "location": "' . $where->valueString . '" }';
    	}
    $comma = ', ' . "\n";
  }
  echo '] }';
}      
      
      // load ZEND Google Calendar classes
      require_once 'Zend/Loader.php';
      Zend_Loader::loadClass('Zend_Gdata');
      Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
      Zend_Loader::loadClass('Zend_Gdata_Calendar');
      Zend_Loader::loadClass('Zend_Http_Client');
      
      // connect to Google Calendar service
      $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
      $user = "biking2010@gmail.com";
      $pass = "foothill2010";
      $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
      outputCalendar($client);
 		
?>


